
class TestShader2 extends egret.DisplayObjectContainer {
    constructor() {
        super();

        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private poke1: egret.Bitmap;
    private poke2: egret.Bitmap;
    private poke3: egret.Bitmap;

    private lb: eui.Label;
    private lb2: eui.Label;

    private onAddToStage(e: egret.Event) {
        this.createScene();
    }
    
    createScene(){
        this.poke1 = new egret.Bitmap(RES.getRes("poke0_png"))
        this.addChild(this.poke1);
        this.poke2 = new egret.Bitmap(RES.getRes("poke0_png"))
        this.addChild(this.poke2);
        this.poke3 = new egret.Bitmap(RES.getRes("poke0_png"))
        this.addChild(this.poke3);

        this.poke1.x = this.stage.stageWidth / 2 - this.poke1.width ;
        this.poke1.y = this.stage.stageHeight / 2 - this.poke1.height / 2;// - 300;
        this.poke2.x = this.poke1.x-200;
        this.poke2.y = this.poke1.y-200;
        this.poke3.x = this.poke1.x+200;
        this.poke3.y = this.poke1.y+200;

        this.lb = new eui.Label();
        this.lb.text = "test timer";
        this.addChild(this.lb);
        this.lb.x = 200;
        this.lb.y = 200;

        this.lb2 = new eui.Label();
        this.lb2.text = "test etimer";
        this.addChild(this.lb2);
        this.lb2.x = 200;
        this.lb2.y = 240;

        this.addShader();

        this.startTimer();
        this.startETimer()
    }

    timer: TimersObj;
    timerid = 0;
    timerStart : Date;

    etimerStart : Date;
    etimerid = 0;
    startETimer(){
        let etimer = new egret.Timer(3000,1);
        etimer.start();
        this.etimerStart = new Date();
        this.etimerid++;
        etimer.addEventListener('timer',this.onETimer,this)
    }

    startTimer(){
        if (!this.timer){
            this.timer = new TimersObj();
        }
        this.timerStart = new Date();
        this.timer.setTimeout(String(this.timerid++),this.onTimer,this,3000);        
    }

    onETimer(){
        let cost = new Date().getTime() - this.etimerStart.getTime();
        this.lb2.text = (this.etimerid-1) + ":" + String(cost);
        this.startETimer();
    }

    onTimer(){
        let cost = new Date().getTime() - this.timerStart.getTime();
        this.lb.text = (this.timerid-1) + ":" + String(cost);
        this.startTimer();
    }

    private async addShader() {
        let vertexSrc = `
            attribute vec2 aVertexPosition;
            attribute vec2 aTextureCoord;
            attribute vec2 aColor;

            uniform vec2 projectionVector;

            varying vec2 vTextureCoord;
            varying vec4 vColor;

            const vec2 center = vec2(-1.0, 1.0);

            void main(void) {
                gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);
                vTextureCoord = aTextureCoord;
                vColor = vec4(aColor.x, aColor.x, aColor.x, aColor.x);
            }
        `;

        let fragmentSrcr = `
            precision lowp float;
            varying vec2 vTextureCoord;
            varying vec4 vColor;
            uniform sampler2D uSampler;

            uniform float angle;
            uniform float scale;

            void main() {
                gl_FragColor = vec4(1.0,0.0,0.0,1.0);
            }
        `;
        let fragmentSrcg = `
            precision lowp float;
            varying vec2 vTextureCoord;
            varying vec4 vColor;
            uniform sampler2D uSampler;

            uniform float angle;
            uniform float scale;

            void main() {
                gl_FragColor = vec4(0.0,1.0,0.0,1.0);
            }
        `;
        let fragmentSrcb = `
            precision lowp float;
            varying vec2 vTextureCoord;
            varying vec4 vColor;
            uniform sampler2D uSampler;

            uniform float angle;
            uniform float scale;

            void main() {
                gl_FragColor = vec4(0.0,0.0,1.0,1.0);
            }
        `;

        let rFilter = new egret.CustomFilter(vertexSrc, fragmentSrcr);
        let gFilter = new egret.CustomFilter(vertexSrc, fragmentSrcg);

        //let str = await RES.getResByUrl("resource/shader/egret/s2_frag.glsl",null,null,RES.ResourceItem.TYPE_TEXT);
        //let bFilter = new egret.CustomFilter(vertexSrc, fragmentSrcb);
        //let bFilter = new egret.CustomFilter(vertexSrc, str);
        //this.poke1.filters = [rFilter];
        //this.poke2.filters = [gFilter];
        //this.poke3.filters = [bFilter];
        let s = new PixelShader(this.poke3);
        s.setShader("egret/s2_frag");

        let s1 = new PixelShader(this.poke1);
        s1.setShader("shadertoy/water");

        let s2 = new PixelShader(this.poke2);
        s2.setShader("trans/ink");
        
    }
}